<template>
    <!-- 章节列表 -->
    <!-- 添加和修改章节表单 -->
    <el-dialog title="添加章节" @close="close()" :visible.sync="dialogChapterFormVisible">
        <el-form :model="chapter" label-width="120px">
            <el-form-item label="章节标题" :label-width="formLabelWidth">
                <el-input v-model="chapter.title" />
            </el-form-item>
            <el-form-item label="章节排序" :label-width="formLabelWidth">
                <el-input v-model="chapter.sort" :min="0" controls-position="right" />
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="close()">取 消</el-button>
            <el-button type="primary" @click="saveOrUpdateChapter">确 定</el-button>
        </div>
    </el-dialog>
</template>

<script>
import chapter from "@/api/teacher/chapter"
export default {
    data() {
        return {
            dialogChapterFormVisible:false,
             chapter:{
                title:'',
                sort:0
            }
        }
    },
    methods: {
        open(chapterId){
            this.dialogChapterFormVisible = true
            if(chapterId){
                chapter.queryByChapterId(chapterId)
                .then(res => {
                    this.chapter = res.data.chapter
                })
            }
        },
        close(){
            this.dialogChapterFormVisible = false
            this.reset()
        },
        reset(){
            this.chapter = {
                title:'',
                sort:0
            }
        },
        //添加章节
        addChapter(){
            //设置课程id到chapter对象里
            this.chapter.courseId = this.$parent.$parent.courseId
            chapter.addChapter(this.chapter)
            .then(res => {
                //关闭弹窗
               this.close()
                //提示信息
                this.$message({
                    type:'success',
                    message:'添加章节成功!'
                })
                //刷新页面
                this.$parent.getChapterVideo()
            })
        },
        updateChapter(){
            chapter.updateChapter(this.chapter)
            .then(res => {
                //关闭弹窗
                this.close()
                //提示信息
                this.$message({
                    type:'success',
                    message:'修改章节成功!'
                })
                //刷新页面
                this.$parent.getChapterVideo()
            })
        },
        saveOrUpdateChapter(){
            if(!this.chapter.id){
                this.addChapter()
            }else{
                this.updateChapter()
            }
        },
    },
}
</script>
